<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" id="num" placeholder="输入一位四位数" />
    <button id="btn">加密计算</button>
    <input type="text" id="rea" placeholder="加密结果" />
    <script>
      function calc(arr) {
        var a = [0, 0, 0, 0];
        for (var i = 0; i < arr.length; ++i) {
          a[i] = (parseInt(arr[i]) + 5) % 10; //预处理数据
        }
        //交换数据
        var tmp;
        for (var i = 0; i < arr.length / 2; ++i) {
          tmp = a[i];
          a[i] = a[arr.length - i - 1];
          a[arr.length - i - 1] = tmp;
        }
        return a;
      }
      //第二种
      //     function getNumber(num) {
      //         num = num + "";
      //         var a = num[0];
      //         var b = num[1];
      //         var c = num[2];
      //         var d = num[3];
      //         a = (parseInt(a) + 5) % 10;
      //         b = (parseInt(b) + 5) % 10;
      //         c = (parseInt(c) + 5) % 10;
      //         d = (parseInt(d) + 5) % 10;
      //         console.log(a, b, c, d);
      //         return parseInt(d + "" + c + "" + b + "" + a);
      //     }

      btn.onclick = function () {
        var arr = num.value; //获取输入框数据
        rea.value = calc(arr);
      };
    </script>
  </body>
</html>
